package com.aaa.three.service.impl;

import com.aaa.three.entity.AddDate;
import com.aaa.three.entity.DoctorList;
import com.aaa.three.entity.DoctorSchedule;
import com.aaa.three.entity.ScheduleVo;
import com.aaa.three.mapper.SchduleDepartmentMapper;
import com.aaa.three.mapper.ScheduleMapper;
import com.aaa.three.pojo.domain.AdminDept;
import com.aaa.three.pojo.domain.AdminInfo;
import com.aaa.three.pojo.domain.AdminOffice;
import com.aaa.three.pojo.domain.AdminRole;
import com.aaa.three.pojo.query.DoctorQuery;
import com.aaa.three.result.AjaxResult;
import com.aaa.three.service.ScheduleService;
import com.aaa.three.sysconst.LoginConst;
import com.aaa.three.util.ArraysDate;
import com.aaa.three.util.QueryDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @PACKAGE_NAME: com.aaa.three.service.impl
 * @NAME: ScheduleServiceImpl
 * @USER: Tprairie
 * @DATE: 2022/1/13
 * @PROJECT_NAME: three
 */
@Service
public class ScheduleServiceImpl implements ScheduleService {
    @Autowired
    private ScheduleMapper scheduleMapper;
    @Autowired
    private SchduleDepartmentMapper schduleDepartmentMapper;
    @Autowired
    private HttpSession session;

    @Override
    public AjaxResult addDate() {
        String day = QueryDate.query(6);
        AddDate listdate=scheduleMapper.getDate(day);
        if(listdate==null){
            Integer i= scheduleMapper.addDate(day);
            return AjaxResult.success();
        }
        return AjaxResult.fail("日期已存在");
    }


    @Override
    public AjaxResult listAll(DoctorQuery d) {
        AdminInfo attribute = (AdminInfo) session.getAttribute(LoginConst.EMP_IN_SESSION);
        AdminRole role = attribute.getRole();
        Integer roleId = role.getId();

        AdminDept dept = attribute.getDept();
        Integer did = dept.getId();

        AdminOffice office = attribute.getOffice();
        Integer oid = office.getId();
        d.setRoleId(roleId);
        d.setOid(oid);
        d.setDid(did);
        String format = QueryDate.query(0);
        d.setNewDate(format);
        List<DoctorSchedule> doctorSchedules = scheduleMapper.listAll(d);
        Integer sum = scheduleMapper.listSum(d);
        ArrayList query = ArraysDate.query();
        HashMap<Object, Object> map = new HashMap<>();
        map.put("sche",doctorSchedules);
        map.put("sum",sum);
        map.put("listDate",query);
        return AjaxResult.success(map);
    }
    @Override
    public AjaxResult addSchedule(ScheduleVo scheduleVo) {
        List<DoctorSchedule> doctor=scheduleMapper.listAddSchedule(scheduleVo);
        if(doctor.size()>0){
            return AjaxResult.fail("该时间已排班");
        }
        Integer uptotalnumber = scheduleVo.getUptotalnumber();
        Integer downtotalnumber = scheduleVo.getDowntotalnumber();
        Integer to=uptotalnumber+downtotalnumber;
        scheduleVo.setTotalnumber(to);
        Integer a = scheduleMapper.addSchedule(scheduleVo);
        if(a>0){
            return AjaxResult.success();
        }
        return AjaxResult.fail("排班失败");
    }

    @Override
    public AjaxResult getDeleteDate() {
        String query = QueryDate.query(1);
        List<AddDate> deleteDate = schduleDepartmentMapper.getAddschedule(query);
        return AjaxResult.success(deleteDate);
    }

    @Override
    public AjaxResult deleteSchedule(ScheduleVo scheduleVo) {
        List<ScheduleVo> scheduleVo1=scheduleMapper.listDelete(scheduleVo);
        if(scheduleVo1.size()==0){
            Integer a=scheduleMapper.deleteSchedule(scheduleVo);
            if(a>0){
                return AjaxResult.success();
            }
            return AjaxResult.fail("该日期未排班");
        }
        return AjaxResult.fail("该日期已有挂诊");
    }

    @Override
    public AjaxResult getSchedule() {
        String query = QueryDate.query(0);
        List<AddDate> addschedule = schduleDepartmentMapper.getAddschedule(query);
        if(addschedule.size()>0){
            return AjaxResult.success(addschedule);
        }
        return AjaxResult.fail("获取时间失败");
    }

    @Override
    public AjaxResult getNumber(ScheduleVo scheduleVo) {
        ScheduleVo scheduleVo1=scheduleMapper.getNumber(scheduleVo);
        return AjaxResult.success(scheduleVo1);
    }

    @Override
    public AjaxResult updateSchedule(ScheduleVo schedule) {
        DoctorList doctorList = scheduleMapper.getUpdateSchedule(schedule);
        if(doctorList!=null){
            Integer a=scheduleMapper.updateSchedule(schedule);
            if(a>0){
                return AjaxResult.success();
            }
            return AjaxResult.fail("修改排班失败");
        }

        return AjaxResult.fail("当前时间未排班");
    }



}
